package com.sven.media;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sven.media.utils.NumUtil;
import com.sven.media.utils.TimeUtil;
import com.sven.media.widget.WaveLineView;

import java.io.IOException;

/**
 * Created by Sven on 2016/10/13 0013.
 */

public class VoicePlayer extends RelativeLayout {

    private ImageView mIv_Play;
    private TextView mTv_Time;
    private WaveLineView waveLineView;
    private VoicePlayer layout_player;
    private VoiceClickListener voiceClickListener;

    public VoicePlayer(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.view_player, this);
    }

    public VoicePlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_player, this);
    }

    public VoicePlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_player, this);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public VoicePlayer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        LayoutInflater.from(context).inflate(R.layout.view_player, this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mIv_Play = (ImageView) findViewById(R.id.iv_play);
        mTv_Time = (TextView) findViewById(R.id.tv_time);
        waveLineView = (WaveLineView) findViewById(R.id.waveline);
        layout_player = (VoicePlayer) findViewById(R.id.layout_player);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ViewGroup.LayoutParams IvPlayParams = mIv_Play.getLayoutParams();
        ViewGroup.LayoutParams TvTimePrams = mTv_Time.getLayoutParams();
        ViewGroup.LayoutParams WaveParams = waveLineView.getLayoutParams();
        int rootW = layout_player.getMeasuredWidth();
        int rootH = layout_player.getMeasuredHeight();
        IvPlayParams.width = rootH / 2;
        IvPlayParams.height = rootH / 2;
        TvTimePrams.height = rootH / 3;
        WaveParams.height = rootH / 3;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    public void play(String recordPath, long duration) {
        voiceClickListener = new VoiceClickListener(recordPath);
        mIv_Play.setOnClickListener(voiceClickListener);
        mTv_Time.setText(TimeUtil.toTimeStr(NumUtil.float2Int(duration)));
    }

    public void stop() {
        if (voiceClickListener.getMp() != null) {
            voiceClickListener.getMp().release();
        }
    }

    class VoiceClickListener implements OnClickListener {
        private MediaPlayer mp;

        public MediaPlayer getMp() {
            return mp;
        }

        String url;

        public VoiceClickListener(String url) {
            this.url = url;
        }

        @Override
        public void onClick(View v) {
            try {
                v.setClickable(false);
                mp = new MediaPlayer();
                mp.setDataSource(url);
                mp.prepare();
                mp.start();
            } catch (IllegalArgumentException | IllegalStateException | IOException e) {
                e.printStackTrace();
            }
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp1) {
                    mp1.release();
                    v.setClickable(true);
                }
            });
        }
    }


}
